package com.pm.service.supervision.Impl;

import com.pm.entity.po.file.FileLib;
import com.pm.entity.po.supervision.FrontLineSupervision;
import com.pm.entity.po.supervision.FrontLineSupervisionCompany;
import com.pm.entity.vo.supervision.FrontLineSupervisionVo;
import com.pm.mapper.file.FileLibMapper;
import com.pm.mapper.supervision.FrontLineSupervisionCompanyMapper;
import com.pm.mapper.supervision.FrontLineSupervisionMapper;
import com.pm.service.impl.BaseServiceImpl;
import com.pm.service.supervision.SupervisionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @program: pm_info_api
 * @description:
 * @author: xzj
 * @create: 2020-08-26 11:46
 **/
@Service
public class SupervisionServiceImpl extends BaseServiceImpl<FrontLineSupervision> implements SupervisionService {
    @Autowired
    FrontLineSupervisionMapper frontLineSupervisionMapper;
    @Autowired
    FrontLineSupervisionCompanyMapper frontLineSupervisionCompanyMapper;
    @Autowired
    FileLibMapper fileLibMapper;

    @Resource
    private void setBaseMapper(FrontLineSupervisionMapper frontLineSupervisionMapper) {
        super.setBaseMapper(frontLineSupervisionMapper);
    }

    @Override
    public Integer addSupervision(FrontLineSupervision frontLineSupervision) {
        // 文件信息
        FileLib fileLib = new FileLib();
        fileLib.setUserId(frontLineSupervision.getAddUser());
        fileLib.setType(frontLineSupervision.getFileLibType());
        // 如果数据库中存在删除
        if (frontLineSupervision.getId() != null) {
            frontLineSupervisionMapper.delete(frontLineSupervision.getId());
            frontLineSupervisionCompanyMapper.delete(frontLineSupervision.getId());
            fileLibMapper.deleteSupervision(frontLineSupervision.getId());
        }

        // 一级督导新增
        Integer result = frontLineSupervisionMapper.add(frontLineSupervision);
        fileLib.setSecondAimsId(frontLineSupervision.getId());
        // 添加责任单位
        for (FrontLineSupervisionCompany company : frontLineSupervision.getFrontLineSupervisionCompanies()) {
            // 一级督导Id
            company.setSupervisionId(frontLineSupervision.getId());
            frontLineSupervisionCompanyMapper.add(company);
            if (frontLineSupervision.getFiles().size() != 0) {
                fileLib.setFiles(frontLineSupervision.getFiles());
                fileLib.setAimsId(company.getId());
            }
            fileLibMapper.add(fileLib);
        }
        return result;
    }

    @Override
    public boolean delete(Integer id) {

        boolean ok = frontLineSupervisionCompanyMapper.delete(id);
        if (ok == true) {
            return frontLineSupervisionMapper.delete(id);
        } else {
            return false;
        }
    }

    @Override
    public boolean updatesupervision(FrontLineSupervision frontLineSupervision) {
        if (frontLineSupervision.getFiles().size() != 0) {
            FileLib fileLib = new FileLib();
            fileLib.setUserId(frontLineSupervision.getEditUser());
            fileLib.setFiles(frontLineSupervision.getFiles());
            fileLib.setAimsId(frontLineSupervision.getId());
            fileLib.setType(frontLineSupervision.getFileLibType());
            fileLib.setFileAddTime(new Date());
            fileLibMapper.add(fileLib);
        }
        return frontLineSupervisionMapper.updatesupervision(frontLineSupervision);
    }

    @Override
    public List<FrontLineSupervisionVo> loadOne(FrontLineSupervision frontLineSupervision) {
        return frontLineSupervisionMapper.loadOne(frontLineSupervision);
    }
}
